<%
/*
Copyright 2010-2011 CumulusCloud
http://cumuluscloud.cc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

%>
<%@page import="cumulusCloud.common.ManageUser"%>
<%@page import="cumulusCloud.common.objects.CloudUser"%>
<%
if (ManageUser.listUsers().length != 0) {
	response.sendRedirect("/");
	return;
}

%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="cumulusCloud.common.HtmlComponents"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%=HtmlComponents.headBannerCSS(false)%>
<%=HtmlComponents.headApplicationIcon("ROOT", false)%>
<title>CumulusCloud</title>

<script type="text/javascript">
function validate_add() {
	if (document.formadd.user.value.length == 0) {
		alert('No username entered');
		return false;
	}

	if (document.formadd.password.value.length == 0) {
		alert('No password entered');
		return false;
	}

	if (document.formadd.password.value != document.formadd.confpassword.value) {
		alert('The passwords does not correspond');
		return false;
	}
	return true;
}
</script>

</head>
<body>
<%=HtmlComponents.bodyTopBanner("First Run", false, false)%>

<%
String action = request.getParameter("action");

if ("add".equals(action)) {
	String user = request.getParameter("user");
	String password = request.getParameter("password");
	if ((user != null) && (user.length() != 0) && (password != null) && (password.length() != 0)) {
		CloudUser newUser = new CloudUser(user, password, "EN", true);
		if (ManageUser.createUser(newUser)) {
			response.sendRedirect("/");
		} else {
			out.println("<p style=\"color: red\">Adding the user: " + user + " FAILED</p>");
		}
	}
}
%>

<h1>Create a new user</h1>
<form name="formadd" method="post" action="" onsubmit="return validate_add()">
<p><input name="action" type="hidden" value="add"/>
Username: <input name="user" type="text" /><br />
Password: <input name="password" type="password" /><br />
Confirm Password: <input name="confpassword" type="password" /><br />
<input type="submit" value="Create"/>
</p>
</form>
</body>
</html>